<?php
require("../../../../../libraries/config.php");
require("../../../../../libraries/dbclass2.php");
function xcrud_remove_post($id){
	$db=new db;
	$db->connect();
	$db->query('select * from index_post where id='.$id);
	$rc=$db->fetchOne();
		$pid=$rc['post_id'];
	if($rc['is_image']==1){
	
		$db->nonquery('delete from images where post_id='.$pid);
		$db->nonquery('delete from post_image where id='.$pid);
	}else
	if($rc['is_video']==1){
		$db->nonquery('delete from post_media where id='.$pid);
	}else
	if($rc['is_text']==1){
		$db->nonquery('delete from post_text where id='.$pid);
	}
	$db->close();


}

function xcrud_remove_post_image($id){
	$db=new db;
	$db->connect();
	$db->nonquery('delete from images where post_id='.$id);
	$db->nonquery('delete from index_post where post_id='.$id);
	$db->close();



}
function xcrud_remove_post_video($id){
	$db=new db;
	$db->connect();
	$db->nonquery('delete from index_post where post_id='.$id);
	$db->close();



}

function check_chuong_noidung($data, $primary, $xcrud){
		
	//Kiem tra chuong co image ko, neu co image thi set image = 1 va curl de download image ve-----
	//Kiem tra thu tu chuong
	//Kiem tra permission, user level 4 thi chi duoc quyen update chuong cua minh, con lai duoc quyen update het
	
	//Kiem tra image có không
	$j=check_image($data['truyen_chuong_noidung.noidung']);
	if($j>0){
		$url="content=".urlencode($data['truyen_chuong_noidung.noidung'])."&name=mong.vn";	
		
		$data['truyen_chuong_noidung.noidung']=urldecode(mycurl($url,'http://sv1.khodo.info/khodo/images/text/image.php'));
	
		
		$db=new db;
		$db->connect();
		$db->nonquery('update truyen_chuong set id='.$data['truyen_chuong_noidung.truyen_chuong_id']);
		$db->close();
	}
	return $data;

}

function check_image($data){
$data = mb_convert_encoding($data, 'HTML-ENTITIES', "UTF-8"); 
$dom = new DOMDocument;
 $dom->loadHTML($data); 
 $sxe = simplexml_import_dom($dom);

	$j=0;	
	 foreach ($sxe->xpath('//img[@src]') as $a)
	 {	
		
		$j++;
	 }
 return $j;
 }
 
function mycurl($data,$url){
	//open connection
	$ch = curl_init();
	//set the url, number of POST vars, POST data
	curl_setopt($ch,CURLOPT_URL, $url);
	curl_setopt($ch,CURLOPT_POST, count($data));
	curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);

	//execute post
	$result = curl_exec($ch);
	return $result;
	//close connection
	curl_close($ch);
} 
?>
